home *** CD-ROM | disk | FTP | other *** search
- Path: eddie.Colorado.EDU!rangaswa
- From: rangaswa@eddie.Colorado.EDU (qwertyuiop)
- Newsgroups: comp.lang.c
- Subject: [Q] Experts: Multi-dim dynamic arrays
- Date: 2 Apr 1996 04:57:17 GMT
- Organization: University of Colorado,Boulder
- Distribution: usa
- Message-ID: <4jqc3d$lru@peabody.colorado.edu>
- NNTP-Posting-Host: eddie.colorado.edu
-
- Hi!
-
- I need help with reading in integer data from a table using dynamic
- arrays. For example, consider the following:
-
- 4 4 /* number of rows, columns in the table, will vary for each data set*/
-
- 3 5 7 8 } This is the actual table I want read.
- 1 2 3 4 }
- 5 6 7 8 }
- -1 3 5 -9 }
-
- ---------------------------------------------------------------------------
- Here is a synopsis of the code I tried: I get no compilation errors, but
- during execution, I get floating point exception at the line marked XX.
- (Necessary file pointers, header files etc. are included but not shown here!.)
-
- int **imatrix(int nrl,int nrh,int ncl,int nch);
- main()
- {
-
- int i, j; /* loop variables */
- int R, C; /* number of rows, columns */
- int **Table; /* table of data */
- int X; /* scratch variable */
-
- fscanf(fileptr, "%d %d\n", &R, &C);
- Table = imatrix(1,R,1,C);
- for(i = 0; i < R; i++)
- {
- for(j = 0; j < C; j++)
- {
- fscanf(fileptr, "%d", &X); <------There is no need for X here, still!
- *(*(A+i)+j) = X; XX<------floating point exception occurs here!
- }
- fscanf(fileptr, "\n");
- }
- ....
- }
-
- int **imatrix(int nrl, int nrh, int ncl, int nch) /* see numerical recipes! */
- {
- int i, **m;
- m = (int **)malloc((unsigned)(nrh-nrl+1)*sizeof(int*));
- if(!m) nerror("alloc failure, exiting!..\n");
- m -= nrl;
-
- for(i=nrl;i<=nrh;i++)
- {
- m[i] = (int*)malloc((unsigned)(nch-ncl+1)*sizeof(int));
- if(!m[i]) nerror("alloc failure etc...\n");
- m[i] -= ncl;
- }
- return m;
- }
- ----------------------------------------------------------------------------
- Questions: 1. On my machine, size of int, int * are both 4 bytes. I'm using
- a gnu compiler on a Unix machine. Why do I get a floating point exception
- at the line mentioned above?
- 2. The imatrix function returns a pointer to an array of pointers to rows.
- I ran a quick address and content check to see if my dereferencing of pointers
- is faulty. There is a difference of 32 bytes between the address of Table[i]
- and Table[i+1], how come?
-
- Can any C guru point out what is wrong?
- Thanks a bunch. Please post replies, since this may interest others too.
-
- ~balu/..
-
-
-
-
-